// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Author: Huawei OS Kernel Lab
 * Create: Thu Aug 15 16:14:23 2023
 */

#include <asm/override.h>
#include <linux/mm.h>
#include <asm/mman.h>

void __override zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
		unsigned long size)
{
	if (address < vma->vm_start || address + size > vma->vm_end ||
				!(vma->vm_flags & VM_PFNMAP))
		return;

	liblinux_pal_vm_zap(address, size, LIBLINUX_PAL_VM_TO_USER);
}

int __override remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
		    unsigned long pfn, unsigned long size, pgprot_t prot)
{
	unsigned int flags = LIBLINUX_PAL_REMAP_CACHE;
	unsigned long pgprot = PROT_READ | PROT_WRITE;

	if (pgprot_val(pgprot_writecombine(prot)) == pgprot_val(prot))
		flags = LIBLINUX_PAL_REMAP_NORMAL_NC;

	if ((pgprot_val(pgprot_device(prot)) == pgprot_val(prot)) ||
	    (pgprot_val(pgprot_noncached(prot)) == pgprot_val(prot)))
		flags = LIBLINUX_PAL_REMAP_DEVICE;

	return liblinux_pal_remap_pfn_range(addr, pfn, PAGE_ALIGN(size), pgprot, flags);
}

/*
 * Copy user data from/to a page which is mapped into a different processes
 * address space.  Really, we want to allow our "user space" model to handle
 * this.
 */
void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
		       unsigned long uaddr, void *dst, const void *src,
		       unsigned long len)
{
	memcpy(dst, src, len);
}
